package cn.ffcs.uec.view.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.view.dao.UecStaffAllDao;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
@Repository("uecStaffAllDao")
@Transactional
public class UecStaffAllDaoImpl extends BaseDaoImpl implements UecStaffAllDao {

	@Override
	public UecStaffAll findUecStaffAll(UecStaffAll uecStaffAll) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM VIEW_STAFF_ALL T  WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		List<UecStaffAll> list =null;
		if (uecStaffAll != null) {
				sb.append(" AND T.CODE = ? ");
				params.add(uecStaffAll.getCode().toUpperCase());
				//sb.append(" AND T.NAME = ? ");
				//params.add(uecStaffAll.getName());
			list = this.jdbcFindList(sb.toString(),params, UecStaffAll.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}
	@Override
	public PageInfo queryPageInfoByUecStaffAll(UecStaffAll uecStaffAll,
			int current, int pageSize) {
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		String orgName ="";
		String[] orgnamelist=null;
		String orgNametemp =null;
		if(!StrUtil.isNullOrEmpty(code)) {
			orgNametemp = PermissionUtil.getManagerOrg();
			orgnamelist=orgNametemp.split(",");
			if(orgnamelist != null && orgnamelist.length==1) {
				orgName=orgnamelist[0];
			}
		}
		
		if (null != uecStaffAll) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM VIEW_STAFF_ALL T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			//sb.append(" AND T.STATUS_CD = ? ");
			sb.append(" AND T.CODE not like '%W%' ");
			//params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecStaffAll.getPkpsndoc())) {
				sb.append(" AND T.PKPSNDOC = ? ");
				params.add(uecStaffAll.getPkpsndoc());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffAll.getName())) {
				sb.append(" AND T.NAME like ? ");
				params.add("%"+uecStaffAll.getName()+"%");
			}
			if (!StrUtil.isNullOrEmpty(uecStaffAll.getCode())) {
				sb.append(" AND T.CODE = ? ");
				params.add(uecStaffAll.getCode());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffAll.getForgname())) {
				sb.append(" AND T.FORGNAME = ? ");
				params.add(uecStaffAll.getForgname());
			}
			if (!"区本部".equals(orgName) && !StrUtil.isNullOrEmpty(orgName)) {
				sb.append(" AND T.ORGNAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
			}
				if(orgnamelist !=null && orgnamelist.length>1) {
					sb.append("AND (T.orgName like ? ");
					params.add("%"+orgnamelist[0]+"%");
					for(int i=1;i<orgnamelist.length-1;i++) {
						sb.append(" OR T.orgName like ? ");
						params.add("%"+orgnamelist[i]+"%");
					}
					sb.append(" OR T.orgName like ? )");
					params.add("%"+orgnamelist[orgnamelist.length-1]+"%");
				}
				//用于查询13个组织下的全部人员
				if(!StrUtil.isNullOrEmpty(uecStaffAll.getForgname())) {
						sb.append(" AND T.FORGNAME like ? ");
						params.add("%"+uecStaffAll.getForgname()+"%");
            	}
			sb.append(" order by T.ORGNAME ");
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecStaffAll.class);
		}
		return null;
	}
}
